package zakladki.AS3 {

import mx.collections.*;
import mx.controls.ComboBox;
import mx.controls.Label;
import mx.controls.CheckBox;
import mx.controls.NumericStepper;
import mx.core.FlexGlobals;

public class WarmWaterChangeListener {


    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    private var bathVolumeCombo:ComboBox;
    private var heatingSourceCombo:ComboBox;
    private var acTempCombo:ComboBox;
    private var acFreqCombo:ComboBox;
    private var acDurationCombo:ComboBox;
    private var bathNumberSelection:NumericStepper;
    private var showerNumberSelection:NumericStepper;
    private var acEnabledCheckBox:CheckBox;
    private var acEnabledLabel:Label;
    private var acFreqLabel:Label;
    private var acDurationLabel:Label;
    private var acTemp:Label;

    public function WarmWaterChangeListener() {
    }

    public function initialize(bathVolumeCombo:ComboBox, heatingSourceCombo:ComboBox, acTempCombo:ComboBox, acFreqCombo:ComboBox, acDurationCombo:ComboBox, bathNumberSelection:NumericStepper, showerNUmberSelection:NumericStepper, acEnabledCheckBox:CheckBox, acEnabledLabel:Label, acFreqLabel:Label, acDurationLabel:Label, acTemp:Label) {
        this.bathVolumeCombo = bathVolumeCombo;
        this.heatingSourceCombo = heatingSourceCombo;
        this.acTempCombo = acTempCombo;
        this.acFreqCombo = acFreqCombo;
        this.acDurationCombo = acDurationCombo;
        this.bathNumberSelection = bathNumberSelection;
        this.showerNumberSelection = showerNUmberSelection;
        this.acEnabledCheckBox = acEnabledCheckBox;
        this.acEnabledLabel = acEnabledLabel;
        this.acFreqLabel = acFreqLabel;
        this.acDurationLabel = acDurationLabel;
        this.acTemp = acTemp;
    }

    public function bathNumberChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.bathCounter(selectedData.bathNumber);
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function bathVolumeChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.setBathVolume(selectedData.bathVolume);
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function heatingSourceChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function showerNumberChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.showerCounter(selectedData.showerNumber);
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function acEnabledChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();

        acFreqCombo.visible = !acFreqCombo.visible;
        acFreqLabel.visible = !acFreqLabel.visible;
        acDurationCombo.visible = !acDurationCombo.visible;
        acDurationLabel.visible = !acDurationLabel.visible;
        acTempCombo.visible = !acTempCombo.visible;
        acEnabledLabel.visible = !acEnabledLabel.visible;
        acTemp.visible = !acTemp.visible;
        if(acFreqCombo.visible) {
            mainApp.widok_sceny.acEnabled(selectedData.acTemp,selectedData.acFrequency,selectedData.acDuration);
        }
        else{
            mainApp.widok_sceny.acDisabled();
        }

        if(calc){
            applyChanges(selectedData);
        }
    }

    public function acFrequencyChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.acFrequency(selectedData.acFrequency);
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function acDurationChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.acDuration(selectedData.acDuration);
        if(calc){
            applyChanges(selectedData);
        }
    }

    public function acTempChanged(calc:Boolean):void {
        var selectedData:WarmWaterSelectedData = createSelectedData();
        mainApp.widok_sceny.acTemp(selectedData.acTemp);
        if(calc){
            applyChanges(selectedData);
        }
    }

    private function createSelectedData():WarmWaterSelectedData {
        var bathVolume:int = bathVolumeCombo.selectedItem.data;
        var bathNumber:int = bathNumberSelection.value;
        var showerNumber:int  = showerNumberSelection.value;
        var waterHeatingSource:int = heatingSourceCombo.selectedItem.data;
        var acEnabled:Boolean = acEnabledCheckBox.selected;
        var acFrequency:int = acFreqCombo.selectedItem.data;
        var acDuration:int = acDurationCombo.selectedItem.data;
        var acTemp:int = acTempCombo.selectedItem.data;

        return new WarmWaterSelectedData(bathVolume,bathNumber,showerNumber,waterHeatingSource,acEnabled,acFrequency,acDuration,acTemp);
    }

    private function applyChanges(selectedData:WarmWaterSelectedData): void {
        updatePerson(selectedData);
        mainApp.calculate();
    }

    private function updatePerson(selectedData:WarmWaterSelectedData):void{
        mainApp.updateWarmWater(selectedData);
    }

}
}